package cn.jdy.plugins.wap;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.common.constant.GlobalConstant;
import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.platform.entity.ConfValue;
import cn.jdy.platform.service.ConfService;
import cn.jdy.plugins.entity.HelpInfo;
import cn.jdy.plugins.service.HelpService;

/**
 * @title 文章信息控制类
 * @date 2014-03-12
 * @author YangYi
 */
@Controller
@RequestMapping("/help")
public class HelpCtrl extends BaseCtrl {

	@Autowired
	private HelpService helpService;
	@Autowired
	private ConfService confService;

	/**
	 * 帮助中心列表查询
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/{typeid}.html")
	public ModelAndView list(HelpInfo helpInfo, @PathVariable String typeid) {
		ModelAndView mv = new ModelAndView(super.t + "/help/helpList.jsp");
		String title = "";// 页面显示标题
		try {
			helpInfo.setStatus("1");
			helpInfo.setColumnid(typeid);
			// 设置分页信息
			if (helpInfo.getiDisplayLength() == 0)
				helpInfo.setiDisplayLength(5); // 默认长度5
			if (helpInfo.getPageNo() <= 0)
				helpInfo.setPageNo(1); // 默认页数1
			helpInfo.setiDisplayStart((helpInfo.getPageNo() - 1)
					* helpInfo.getiDisplayLength()); // 显示起始索引
			//查询加入分页信息之后的结果
			mv.addObject("dataList", helpService.query(helpInfo));
			mv.addObject("typeId", typeid);
			mv.addObject("helpInfo", helpInfo);
			mv.addObject("title", title);
			mv.addObject("pageNo", helpInfo.getPageNo());
			mv.addObject("size", helpInfo.getiDisplayLength());
			mv.addObject("total", helpService.count(helpInfo));
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "帮助中心列表查询异常", e);
		}
		return mv;
	}

	/**
	 * 帮助中心详细内容页
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/show-{id}.html")
	public ModelAndView queryDetail(@PathVariable Integer id) {
		ModelAndView mv = new ModelAndView(super.t + "/help/helpDetail.jsp");
		try {
			HelpInfo help = helpService.findOne("id", id, "status", "1");
			help.setColumnname(confService.find(
					Long.valueOf(help.getColumnid())).getValue0());
			mv.addObject("helpInfo", help);
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "查询文章异常", e);
		}
		return mv;
	}

	/**
	 * 帮助分类
	 * 
	 * @return
	 */
	private List<ConfValue> helpClass() {
		List<ConfValue> plist = null;
		try {
			plist = confService.queryByType(GlobalConstant.TYPE_HELP, 0L);// 帮助分类
			for (ConfValue cv : plist) {
				cv.setChildren(confService.findConfValueByPid(cv.getId()));
			}
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "查询帮助分类异常", e);
		}
		return plist;
	}

	/**
	 * 帮助中心根据页码，加载更多
	 * 
	 * @param index
	 * @param helpInfo
	 * @return
	 */
	@RequestMapping("/loadMore/{index}")
	@ResponseBody
	public Map<String, Object> loadMore(@PathVariable("index") int index,
			HelpInfo helpInfo, String typeid) {
		Map<String, Object> resp = new HashMap<>();
		try {
			helpInfo.setStatus("1");
			// 设置分页信息
			if (helpInfo.getiDisplayLength() == 0)
				helpInfo.setiDisplayLength(5);// 默认长度5
			if (index <= 0)
				index = 1;
			helpInfo.setPageNo(index);
			helpInfo.setiDisplayStart((helpInfo.getPageNo() - 1)
					* helpInfo.getiDisplayLength()); // 显示起始索引
			List list = helpService.query(helpInfo, typeid);
			Long cnt = new Long((long) list.size());
			if (list != null && list.size() > 0) {
				resp.put("dataList", list);
				resp.put("countByType", cnt);
				resp.put("statusCode", 200);
			} else {
				resp.put("statusCode", 400);
			}
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "查询帮助异常", e);
		}
		return resp;
	}
}
